/**
 * @Author：ming
 * @Date：2023/9/27 6:08 PM
 * @Filename：Lc36
 */

/**
 * @author ming
 */
public class Lc36 {
    public boolean isValidSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                boolean flag = isTrue(board,i,j);
                if(!flag){
                    return false;
                }
            }
        }
        return true;
    }

    public static  boolean isTrue(char[][] board,int r,int c){
        if(board[r][c]=='.'){
            return true;
        }
        //行
        for (int i = 0; i < 9; i++) {
            if(i!=c){
                if(board[r][i]==board[r][c]){
                    return false;
                }

            }
        }
        //列
        for (int i = 0; i < 9; i++) {
            if(i!=r){
                if(board[i][c]==board[r][c]){
                    return false;
                }
            }
        }

        //九宫

        int startr = (r/3)*3;
        int startc= (c/3)*3;

        for (int i = startr; i < startr+3; i++) {
            for (int j = startc; j < startc+3; j++) {
                if(i!=r&&j!=c){
                    if(board[i][j]==board[r][c]){
                        return false;
                    }
                }
            }
        }


        return true;
    }
}
